// SetFrameRateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "rmsserver.h"
#include "RMSServerDlg.h"
#include "SetFrameRateDlg.h"

#include "../Capture/CapLib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetFrameRateDlg dialog


CSetFrameRateDlg::CSetFrameRateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetFrameRateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetFrameRateDlg)
	m_radioFPS = -1;
	//}}AFX_DATA_INIT
}


void CSetFrameRateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetFrameRateDlg)
	DDX_Radio(pDX, IDC_FPS, m_radioFPS);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetFrameRateDlg, CDialog)
	//{{AFX_MSG_MAP(CSetFrameRateDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetFrameRateDlg message handlers

void CSetFrameRateDlg::OnOK() 
{
	if(UpdateData(TRUE))
	{
		LONG lnRes;
		HKEY hKey;
		DWORD dwDisp;
		DWORD value;

		CRMSServerDlg* dlg = (CRMSServerDlg*)AfxGetMainWnd();

		if(m_radioFPS == 0)
		{
			if(capPVR_SetFrameRate(15))
			{
				if(dlg)
				{
					dlg->m_nCurrentFPS = 15;
				}
			}
		}
		if(m_radioFPS == 1)
		{
			if(capPVR_SetFrameRate(30))
			{
				if(dlg)
				{
					dlg->m_nCurrentFPS = 30;
				}
			}
		}

		lnRes = RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Streambox Everywhere\\Settings",0,KEY_ALL_ACCESS,&hKey);
		if(ERROR_SUCCESS != lnRes)
		{
			lnRes = RegCreateKeyEx(HKEY_CURRENT_USER,"Software\\Streambox Everywhere\\Settings",0,NULL,
				REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
			if(ERROR_SUCCESS != lnRes)
			{
				RegCloseKey(hKey);
				return;
			}
		}
		if(dlg)
		{
			value = (DWORD)(dlg->m_nCurrentFPS);
		}
		else
		{
			if(m_radioFPS == 0)
			{
				value = 15;
			}
			if(m_radioFPS == 1)
			{
				value = 30;
			}
		}
		lnRes = RegSetValueEx(hKey,"Frame rate",NULL,REG_DWORD,(BYTE*)&(value),sizeof(DWORD));
		if(ERROR_SUCCESS != lnRes)
		{
			RegCloseKey(hKey);
			return;
		}

		RegCloseKey(hKey);
	}		
	CDialog::OnOK();
}

BOOL CSetFrameRateDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CRMSServerDlg* dlg = (CRMSServerDlg*)AfxGetMainWnd();
	if(dlg)
	{
		if(dlg->m_nCurrentFPS == 15)
		{
			m_radioFPS = 0;
		}
		if(dlg->m_nCurrentFPS == 30)
		{
			m_radioFPS = 1;
		}
	}
	UpdateData(FALSE);
		
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
